﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;

public partial class admin_Product_detailProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        string st = "select * from TheStorage where ProductName =@p";

        SqlCommand cmd1 = new SqlCommand(st, conn1);
        cmd1.Parameters.Add("@p", Session["name"].ToString());
        conn1.Open();
        cmd1.ExecuteNonQuery();






       
        
        SqlDataReader dr = cmd1.ExecuteReader();
        
        while (dr.Read())
        {


            string name = (string)dr["Image"];
            
            {
                imgProduct.ImageUrl = name;
            }
        }



        conn1.Close();
        lbIDtype.Text = Session["id"].ToString();
        txtName.Text = Session["name"].ToString();
        txtQuantity.Text = Session["Quantity"].ToString();
        txtProduct_SaleOff.Text = Session["saleoff"].ToString();
        txtPrice.Text = Session["price"].ToString();
        txtDetails.Text = Session["detail"].ToString();
        ddlType.SelectedValue = Session["typeid"].ToString();

        
    }
   
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        txtName.Enabled = true;
        txtQuantity.Enabled = true;
        txtProduct_SaleOff.Enabled = true;
        txtPrice.Enabled = true;
        txtDetails.Enabled = true;
        fuImage.Visible = true;
        ddlType.Enabled = true;
        btnCancel.Visible = true;
        btnSave.Visible = true;
        btnUpdate.Visible = false;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        string str = "Update TheStorage Set ProductName='@name',Quantity='@quan',Price='@Price',Product_SaleOff='@prosale',Status='@s',Details='@Details',Image='@img',TypeProduct_ID='@typeProduct' Where ID_Product='@id' ";
        SqlCommand cmd = new SqlCommand(str, conn);
        cmd.Parameters.Add("s", lbIDtype.Text);
        cmd.Parameters.Add("id", lbIDtype.Text);
        cmd.Parameters.Add("@name", txtName.Text);
        cmd.Parameters.Add("@quan", Convert.ToInt32(txtQuantity.Text));
        cmd.Parameters.Add("@Price", Convert.ToInt32(txtPrice.Text));
        cmd.Parameters.Add("@prosale", Convert.ToInt32(txtProduct_SaleOff.Text));
        cmd.Parameters.Add("@Details", txtDetails.Text);
        cmd.Parameters.Add("@typeProduct", ddlType.SelectedItem.Value);
        if (fuImage.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuImage.FileName);
                fuImage.SaveAs(Server.MapPath(".\\image\\") + filename);
               
                cmd.Parameters.Add("img", "~/admin/Product/image/" + filename);
            }
            catch (Exception ex)
            {
                Response.Write("Lỗi" + ex);
            }
        }
       
        

        conn.Open();

        cmd.ExecuteNonQuery();
        Response.Write(lbIDtype.Text);
        Response.Write(txtName.Text);
        Response.Write(txtQuantity.Text);
        Response.Write(txtPrice.Text);
        Response.Write(txtProduct_SaleOff.Text);
        Response.Write(txtDetails.Text);
        Response.Write(ddlType.SelectedValue);
        Response.Write(imgProduct.ImageUrl);
           Response.Write(lbIDtype.Text);

        
        
        
    }




}
